package com.medical.patient.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;
import com.medical.patient.config.HttpUtils;
import com.medical.patient.entity.PatientWalletEntity;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.medical.patient.entity.PatientBankEntity;
import com.medical.patient.service.PatientBankService;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-08-19 15:38:09
 */
@RestController
@RequestMapping("patient/patientbank")
public class PatientBankController {
    @Autowired
    private PatientBankService patientBankService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = patientBankService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 银行卡绑定
     */
    @RequestMapping("/saveBankAccount")
    public R saveBankAccount(@RequestBody PatientBankEntity patientBank){
        patientBankService.save(patientBank);

        return R.ok();
    }

    /**
     * api接口调用
     */
    @RequestMapping("/sign")
    public R sign(@RequestParam("card_number") String card_number,@RequestParam("name") String name){
        String host = "https://dfbank2.market.alicloudapi.com";
        String path = "/verify_name_bankcard";
        String method = "POST";
        String appcode = "210a682a30c6432fbd731a5036549e9f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("card_number", card_number);
        bodys.put("name", name);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok("验证通过");
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{bankId}")
    public R info(@PathVariable("bankId") Integer bankId){
		PatientBankEntity patientBank = patientBankService.getById(bankId);

        return R.ok().put("patientBank", patientBank);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PatientBankEntity patientBank){
		patientBankService.save(patientBank);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PatientBankEntity patientBank){
		patientBankService.updateById(patientBank);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] bankIds){
		patientBankService.removeByIds(Arrays.asList(bankIds));

        return R.ok();
    }

}
